<?php 
if(!defined('BASEPATH'))exit('No diresct script access not allowed');
class Features_model extends CI_Model{
    
    function _construct()
    {
        parent::__construct();
        log_message('debug','Feature model initialized');
    }
    //-----Insert features group
    function features_group_insert($type=NULL,$feature_group=NULL,$code=NULL)
    {
        $sql="insert into feature(type,code,slno,feature) values('$type','$code','0','$feature_group')";
        $query=$this->db->query($sql);
        
    }
    
    //-----Edit features group
    function features_group_modify($type=NULL,$feature_group=NULL,$code=NULL)
    {
        $sql="update feature set feature='$feature_group' where code='$code' and slno=0";
        $query=$this->db->query($sql);

    }
    
    //-----Delete features group
    function features_group_delete($type=NULL,$code=NULL)
    {
        $sql="delete from feature where type='$type' and code='$code'";
        $query=$this->db->query($sql);
        
    }
    
    //-----Insert features 
    function feature_insert($type=NULL,$feature=NULL,$code=NULL,$slno=NULL)
    {
        $sql="insert into feature(type,code,slno,feature) values('$type','$code','$slno','$feature')";
        $query=$this->db->query($sql);
        
    }
    
    //-----Edit features 
    function feature_modify($type=NULL,$feature_group=NULL,$id=NULL)
    {
        $sql="update feature set feature='$feature_group' where id=$id";
        $query=$this->db->query($sql);

    }
    //-----Delete feature 
    function feature_delete($type=NULL,$id=NULL)
    {
        $sql="delete from feature where type='$type' and id='$id'";
        $query=$this->db->query($sql);
        
    }
    
    //----------All row return
    function get_features_groups($type=NULL,$order_type=NULL, $num=NULL, $offset=NULL)
    {
        $subcond = " where slno='0' ";
        $limit = '';
        if($type!=NULL)
            $subcond.=" and type='$type'";
        if ($num > 0)
            $limit = ' limit ' . $offset . ',' . $num;
        $sql="select * from feature ".$subcond. ' order by feature ' . $order_type . $limit;
        $query=$this->db->query($sql);
        $result=$query->result();
        return $result;
       
        
    }
    
    //----------Single row return
    function get_features_group($type=NULL)
    {
        
        
    }
    
    //----------Delete all features
    function get_features($type=NULL,$code=NULL,$order_type=NULL, $num=NULL, $offset=NULL)
    {
        $subcond = ' where slno<>0 ';
        
        if($type!=NULL)
            $subcond.=" and type='$type'";
        if($code!=NULL && is_numeric($code) && $code>0)
            $subcond.=" and code=$code";
        if ($num > 0)
            $limit = ' limit ' . $offset . ',' . $num;
        $sql="select * from feature ".$subcond. ' order by feature ' . $order_type . $limit;
        $query=$this->db->query($sql);
        $result=$query->result();
        return $result;
       
    }
    
    //----------Delete one features row
    function get_feature($type=NULL,$code=NULL)
    {
       
    }
    //--- To fatch highest slno from feature 
     function get_max_slno($code=NULL)
    {
        $sql="select max(slno) as no from feature where code='$code'";
        $query=$this->db->query($sql);
        $row=$query->row();
        return intval($row->no) + 1;
    }
    //--- To fatch highest code no from feature 
    function get_max_code()
    {
        $sql = "select max(code) as no from feature where code<>0 and slno = 0";
        $query=$this->db->query($sql);
        $row=$query->row();
        return intval($row->no) + 1;
    }
}

?>
